$(document).ready(function () {
    $(".dataTables-example").dataTable();
    $("#sizeSelect").selectpicker();
    $("#colorSelect").selectpicker();
    $(window).on('load', function () {
        $("#sizeSelect").selectpicker('val', '');
        $('#sizeSelect').selectpicker('refresh');
    });
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "progressBar": true,
        "positionClass": "toast-top-right",
        "onclick": null,
        "showDuration": "400",
        "hideDuration": "1000",
        "timeOut": "7000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
    /**
     * 分类商品
     */
    $.ajax({
        type: 'get',
        url: "/admin/info/category/list",
        dataType: 'json',
        success: function (data) {
            console.log(data)
            let select = $("#categorySelect");
            for (var i = 0; i < data.length; i++) {
                select.append('<option value="' + data[i].bcatId + '" >' + data[i].bcatDesc + '</option>');
            }
            select.selectpicker('refresh');
            select.selectpicker('render');
        }
    });
    /**
     * 产地
     */
    $.ajax({
        type: 'get',
        url: "/admin/info/place/list",
        dataType: 'json',
        success: function (data) {
            let select = $("#originPlaceSelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].borigId + '" >' + data[i].borigTitle + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        }
    });
    /**
     * 尺寸
     */
    $.ajax({
        type: 'get',
        url: "/admin/info/size/list",
        dataType: 'json',
        success: function (data) {
            console.log(data)
            let param = data[0];
            var select = $("#sizeSelect");
            for (var i = 0; i < data.length; i++) {
                if (data[i] === param) {
                    select.append('<option value="' + data[i].sizeId + '" >' + data[i].sizeDesc + '</option>');
                } else {
                    select.append("<option value='" + data[i].sizeId + "'>"
                        + data[i].sizeDesc + "</option>");
                }
            }
            select.selectpicker('refresh');
            select.selectpicker('render');
        }
    });
    /**
     * 仓库
     */
    $.ajax({
        type: 'get',
        url: "/admin/info/store/list",
        dataType: 'json',
        success: function (data) {
            console.log(data)
            let param = data[0];
            var select = $("#storeSelect");
            for (var i = 0; i < data.length; i++) {
                if (data[i] === param) {
                    select.append('<option value="' + data[i].bstoId + '" >' + data[i].bstoTitle + '</option>');
                } else {
                    select.append("<option value='" + data[i].bstoId + "'>"
                        + data[i].bstoTitle + "</option>");
                }
            }
            select.selectpicker('refresh');
            select.selectpicker('render');
        }
    });
    /**
     * 颜色
     */
    $.ajax({
        type: 'get',
        url: "/admin/color/list",
        dataType: 'json',
        success: function (data) {
            console.log(data)
            let param = data[0];
            var select = $("#colorSelect");
            for (var i = 0; i < data.length; i++) {
                if (data[i] === param) {
                    select.append('<option value="' + data[i].colId + '" >' + data[i].colDesc + '</option>');
                } else {
                    select.append("<option value='" + data[i].colId + "'>"
                        + data[i].colDesc + "</option>");
                }
            }
            select.selectpicker('refresh');
            select.selectpicker('render');
        }
    });
});

/**
 * 保存一个颜色
 */
function saveColor() {
    let colorName = $("#color-name").val();
    if (!colorName){
        toastr.error('颜色名称不能为空！','提示：')
        return ;
    }
    $.post("/admin/color/save", {
        colDesc: colorName
    }, function (resp) {
        if (resp.data) {
            toastr.success(resp.message, '提示');
            $("#myModal").modal('hide');
            setTimeout(function (){
                location.reload()
            },2000)
        } else {
            toastr.error(resp.message, '提示');
        }
    })
}
/**
 * 保存一个商品分类
 */
function saveCategory(){
    let $desc = $('#cat-name')
    let desc = $desc.val();
    if (!desc){
        toastr.error('输入不能为空！','提示：')
        $desc.focus()
        return;
    }
    let $categorySelect = $('#categorySelect')
    $.post("/admin/info/category/save", {
        desc: desc,
    }, function (resp) {
        if (resp.data !== null) {
            toastr.success(resp.message, "提示");
            $desc.val('')
            $('#categoryModal').modal('hide');
            $categorySelect.selectpicker('refresh');
            $categorySelect.selectpicker('render');
            //查找所有的数据动态刷新到表格内
            refreshCategorySelect()
        } else {
            toastr.error(resp.message, '提示')
        }
    })
}

/**
 * 保存一个产地
 */
function saveOriginPlace(){
    let $title = $('#originPlace-title');
    let $categorySelect=$('#categorySelect')
    let title = $title.val()
    if (!title){
        toastr.error('输入不能为空！','提示：')
        $title.focus()
        return;
    }
    $.post("/admin/info/place/save", {
        title:title,
    }, function (resp) {
        if (resp.data !== null) {
            toastr.success(resp.message, "提示");
            $("#originPlace-title").val('')
            $("#originPlace-desc").val('')
            $('#originPlaceModal').modal('hide');
            $categorySelect.selectpicker('refresh');
            $categorySelect.selectpicker('render');
            //查找所有的数据动态刷新到表格内
            refreshOriginPlaceSelect()
        } else {
            toastr.error(resp.message, '提示')
        }
    })
}

/**
 * 保存尺寸信息
 */
function saveSize() {
    let $name = $("#size-name")
    let $sizeSelect = $('#sizeSelect')
    let name = $name.val()
    $.post("/admin/info/size/save", {
        sizeDesc: name,
    }, function (resp) {
        if (resp.data !== null) {
            toastr.success(resp.message, "提示");
            $("#size-name").val('')
            $('#sizeModal').modal('hide');
            $sizeSelect.selectpicker('refresh');
            $sizeSelect.selectpicker('render');
            //查找所有的数据动态刷新到表格内
            refreshSizeSelection()
        } else {
            toastr.error(resp.message, '提示')
        }
    })

}

/**
 * 保存一个仓库信息
 */
function saveStore(){
    let $name = $("#store-name");
    let name = $name.val()
    let $storeSelect = $('#storeSelect')
    $.post("/admin/info/store/save", {
        sizeDesc: name,
    }, function (resp) {
        if (resp.data !== null) {
            toastr.success(resp.message, "提示");
            $("#store-name").val('')
            $('#storeModal').modal('hide');
            $storeSelect.selectpicker('refresh');
            $storeSelect.selectpicker('render');
            //查找所有的数据动态刷新到表格内
            refreshStoreSelection()
        } else {
            toastr.error(resp.message, '提示')
        }
    })
}
/**
 * 动态刷新所有分类item到下拉框内
 */
function refreshCategorySelect(){
    $.ajax({
        type: 'get',
        url: "/admin/info/category/list",
        dataType: 'json',
        success: function (data) {
            let select = $("#categorySelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].bcatId + '" >' + data[i].bcatDesc + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        },
    });
}

/**
 * 动态刷新下拉框-产地
 */
function refreshOriginPlaceSelect(){
    $.ajax({
        type: 'get',
        url: "/admin/info/place/list",
        dataType: 'json',
        success: function (data) {
            let select = $("#originPlaceSelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].borigId + '" >' + data[i].borigTitle + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        },
    });
}
function refreshSizeSelection() {
    $.ajax({
        type: 'get',
        url: "/admin/info/size/list",
        dataType: 'json',
        success: function (data) {
            let select = $("#sizeSelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].bsizeId + '" >' + data[i].bsizeDesc + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        },
    });
}
function refreshColorSelection(){
    $.ajax({
        type: 'get',
        url: "/admin/color/list",
        dataType: 'json',
        success: function (data) {
            var select = $("#colorSelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].colId + '" >' + data[i].colDesc + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        }
    });
}
function refreshStoreSelection(){
    $.ajax({
        type: 'get',
        url: "/admin/info/store/list",
        dataType: 'json',
        success: function (data) {
            var select = $("#storeSelect");
            let deviceStr = "";
            for (let i = 0; i < data.length; i++) {
                deviceStr+='<option value="' + data[i].bstoId + '" >' + data[i].bstoTitle + '</option>';
            }
            select.html('')
            select.append(deviceStr)
            select.selectpicker('refresh');//对searchPayState这个下拉框进行重置刷新
        }
    });
}









/**
 * 设置商品尺寸 abandon
 */
function chooseSizes4Product() {
    //批量删除
    let selects = [];
    let proId = $("#hidden-pro-id").val();
    let count = 0;
    $("input[name='inp-check-box']:checked").each(function () {
        if ($(this).prop('checked')) {
            //如果当前属性为checked
            selects[count++] = $(this).val();
        }
    });
    if (selects.length === 0) {
        swal("您没有选中任何数据，请选中后重试！");
    }
    $.post("/admin/product/saveSize4Product", {
        selects: selects,
        proId: proId
    }, function (resp) {
        if (resp.data) {
            toastr.success(resp.message, "提示");
        } else {
            toastr.error(resp.message, '提示')
        }
    })
}

/**
 * 保存一个商品信息
 */
function saveBackProduct(){
    let $proId = $("#bproId")
    let $proName = $("#proName")
    let $proDesc = $("#proDesc")
    let $proUnion = $("#proUnionSelect")
    let $color = $("#colorSelect")
    let $proInputPrice = $("#proInputPrice")
    let $proOutputPrice = $("#proOutputPrice")
    let $proPredictPrice = $("#proPredictPrice")
    let $proNumber = $("#proNumber")
    let $categorySelect = $("#categorySelect")
    let $originPlaceSelect = $("#originPlaceSelect")
    let $sizeSelect = $("#sizeSelect")
    let $storeSelect = $("#storeSelect")
    let $proBackUpContent = $("#proBackUpContent")
    if (!isValid($proName.val())){
        toastr.error('商品名称不能为空','提示：')
        $proName.focus()
        return;
    }
    if (!isValid($proInputPrice.val())){
        toastr.error('商品进价不能为空','提示：')
        $proInputPrice.focus()
        return ;
    }
    if (!isValid($proOutputPrice.val())){
        toastr.error('商品售价不能为空','提示：')
        $proOutputPrice.focus()
        return ;
    }
    if (!isValid($proPredictPrice.val())){
        toastr.error('商品预测价格不能为空','提示：')
        $proPredictPrice.focus()
        return ;
    }
    if (!isValid($proNumber.val())){
        toastr.error('商品库存数量不能为空','提示：')
        $proNumber.focus()
        return ;
    }
    if (!isValid($categorySelect.val())){
        toastr.error('商品类别不能为空','提示：')
        $categorySelect.focus()
        return ;
    }
    if (!isValid($originPlaceSelect.val())){
        toastr.error('商品产地不能为空','提示：')
        $originPlaceSelect.focus()
        return ;
    }
    if (!isValid($sizeSelect.val())){
        toastr.error('商品尺寸不能为空','提示：')
        $sizeSelect.focus()
        return ;
    }
    if (!isValid($color.val())){
        toastr.error('商品颜色不能为空','提示：')
        $color.focus()
        return;
    }
    if (!isValid($storeSelect.val())){
        toastr.error('商品所属仓库不能为空','提示：')
        $storeSelect.focus()
        return ;
    }
    //封装成为对象
    let backProduct = {}
    backProduct.bproId=$proId.val()
    backProduct.bproTitle = $proName.val()
    backProduct.bproDesc = $proDesc.val()
    backProduct.bproUnit = $proUnion.val()
    backProduct.colorId = $color.val()
    backProduct.binputPrice = $proInputPrice.val()
    backProduct.boutputPrice = $proOutputPrice.val()
    backProduct.bpredictPrice = $proPredictPrice.val()
    backProduct.bproNum = $proNumber.val()
    backProduct.bcatId = $categorySelect.val()
    backProduct.borigId = $originPlaceSelect.val()
    backProduct.bsizeId = $sizeSelect.val()
    backProduct.bstoId = $storeSelect.val()
    backProduct.bbackup = $proBackUpContent.val()
    console.log(backProduct)
    console.log(JSON.stringify(backProduct));
    $.ajax({
        url: "/admin/info/product/save",
        type: "POST",
        data: JSON.stringify(backProduct),
        contentType: "application/json;charset=UTF-8",
        dataType: "text",
        success: function (resp) {
            let parse = JSON.parse(resp)
            if (parse.data) {
                toastr.success(parse.message, "提示");
                setTimeout(function (){
                    location.reload()
                },2000)
            } else {
                toastr.error(parse.message, '提示')
            }
        }
    });



}

/**
 * 有效值
 * @param value
 * @returns {*}
 */
function isValid(value){
    return value;
}




